package com.atguigu.myjuc.interactive;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// V1 synchronized + wait + notifyAll 传统老版本
/*class AirCondition{
    private int number = 0;
    public synchronized void increment() throws InterruptedException {
        while (number!=0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"\t"+(++number));
        this.notifyAll();
    }
    public synchronized void decrement() throws InterruptedException {
        while (number==0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"\t"+(--number));

    }

}*/

//V2 JUC新版，Lock + condition.await() + condition.signalAll()
class AirCondition{
    private int number = 0;
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {
        lock.lock();
        try {
            while (number!=0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
        }finally {
            condition.signalAll();
        }
    }

    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            while (number==0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"\t"+(--number));
        }finally {
            condition.signalAll();
        }
    }
}


/**
 *
 * 有一个初始值为零的变量，现有两个线程对该变量操作，
 * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
 *
 *  *  笔记：Java里面如何进行工程级别的多线程编写
 *  1 多线程变成模板（套路）
 *    高内聚低耦合前提下，线程    操作    资源类
 *
 *  2 多线程交互问题时， 判断    干活    通知
 *
 *  3 需要注意防止虚假唤醒，wait方法需要用到while
 *
 *
 */

public class ProdConsumerDemo {

    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();


        new Thread(()->{

                try {
                    for (int i = 1; i <=10; i++) {
                        TimeUnit.SECONDS.sleep(1);
                        airCondition.increment();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }


        },"A").start();

        new Thread(()->{
            try {
                for (int i = 1; i <=10; i++) {
                    TimeUnit.SECONDS.sleep(1);
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        },"B").start();

        new Thread(()->{
            try {
                for (int i = 1; i <=10; i++) {
                    TimeUnit.SECONDS.sleep(1);
                    airCondition.increment();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        },"C").start();


        new Thread(()->{

            try {
                for (int i = 1; i <=10 ; i++) {
                    TimeUnit.SECONDS.sleep(1);
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        },"D").start();


    }
}
